package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.vo.OrderSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceSupport;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：混元太极门
 * @date ：Created in 2022年06月09日
 * @description ：体检预约 业务
 * @version: 1.0
 */
@RestController
@RequestMapping("/mobile/order")
@Slf4j
public class MobileOrderController {
       @Autowired    //高度封装类
       private RedisTemplate redisTemplate;
       @Autowired    //会员
       private MemberService memberService;
       @Autowired     //预约
       private OrderService orderService;
       @Autowired    //套餐
       private SetMealService setMealService;

       @PostMapping("/submit")
     public Result subimt(@RequestBody OrderSubmitParam param){
        log.info("预约管理信息:",param);
           //redis里面获取验证码
           String validateCode = param.getValidateCode();
             //验证码验证
        String rediskey = RedisConst.VALIDATE_CODE_PREFIX+"_ORDER_"+param.getTelephone();
           String code = redisTemplate.opsForValue().get(rediskey).toString();

           if (!StringUtils.isEmpty(code) && code.equals(validateCode)) {

               //创建会员信息
               Member member =new Member();
               member.setIdCard(param.getIdCard());//身份证号
               member.setSex(param.getSex());//性别
               member.setName(param.getName()); //名字
               member.setPhoneNumber(param.getTelephone());
               // xinxiupdate  这里是任务八更新方法  在controller里自己定义的
               member = memberService.xinxiupdate(member);//添加会员
               //提交体检预约信息
               Order order = new Order();  //体检预约对象
               order.setOrderType(Order.ORDERTYPE_WEIXIN);//微信预约
               order.setSetmealId(param.getSetMealId());//套餐表id
               order.setOrderStatus(Order.ORDERSTATUS_NO);//未到诊 或者已就诊
               order.setOrderDate(param.getOrderDate());  //预约时间
               order.setMemberId(member.getId()); //获取会员表id  这个是跟任务八有关联的自定义方法
               order = orderService.add(order);  //添加预约   add 自定义方法
               //返回结果成功预约
               return  new Result(true, MessageConst.ORDER_SUCCESS,order);


           } else {
               return new Result(false, MessageConst.GET_ORDERSETTING_FAIL);
           }
       }

    /**
     * 根据id查询
     * @param id
     * @return
     */
       @GetMapping("/findById")
    public Result findById(Integer id){
           //查询数据信息
         Order order = orderService.findById(id);//查询预约表id
         Member member = memberService.findById(order.getMemberId());//查询会员表id
         Setmeal setmeal = setMealService.findById(order.getSetmealId()); //查询套餐id 在预约表中id是几
         //返回结果数据
           Map<String,Object> map = new HashMap<>();
           map.put("member",member.getName());  //获取会员表的名字
           map.put("setmeal",setmeal.getName());//获取套餐表的名字
           map.put("orderDate",new SimpleDateFormat("yyyy-MM-dd").format(order.getOrderDate()));
                                         //获取预约表当前预约的时间   例如2022-06-10
           map.put("order",order.getOrderType());//获取体检用户的预约类型  例如:微信或者其他预约方式
           return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,map);
       }
}
